package xyz.xpman.prisonerfitness.activity;

import android.app.Activity;
import android.graphics.Point;
import android.os.Build;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.RequiresApi;

import xyz.xpman.prisonerfitness.R;
import xyz.xpman.prisonerfitness.config.AppConfig;
import xyz.xpman.prisonerfitness.util.ScreenUtil;
import xyz.xpman.prisonerfitness.util.ToastUtil;

public class TrainingTimeIntervalActivity extends Activity {
    private int upAndDownIntervalValue = 0;
    private int groupRestIntervalValue = 0;
    private TextView upAndDownInterval;
    private TextView groupRestInterval;

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_training_time_interval);

        initPopWindow();
        initData();
        initView();
        initEvent();
    }

    private void initPopWindow() {
        // 设置窗口宽度，高度则自适应
        Point windowsSize = ScreenUtil.getWindowsSize(this);
        int width = (int)((float) windowsSize.x * 0.85);
        getWindow().setLayout(width, LinearLayout.LayoutParams.WRAP_CONTENT);
    }

    private void initData() {
        upAndDownIntervalValue = AppConfig.getUpAndDownInterval();
        groupRestIntervalValue = AppConfig.getGroupRestInterval();
    }

    private void initView() {
        upAndDownInterval = findViewById(R.id.up_and_down_interval);
        groupRestInterval = findViewById(R.id.group_rest_interval);


        findViewById(R.id.add_up_and_down_interval).setOnClickListener((view ->
                setUpAndDownInterval(++upAndDownIntervalValue)));

        findViewById(R.id.subtract_up_and_down_interval).setOnClickListener((view ->
                setUpAndDownInterval(--upAndDownIntervalValue)));


        findViewById(R.id.add_group_rest_interval).setOnClickListener((view ->
                setGroupRestInterval(++groupRestIntervalValue)));

        findViewById(R.id.subtract_group_rest_interval).setOnClickListener((view ->
                setGroupRestInterval(--groupRestIntervalValue)));

    }


    @RequiresApi(api = Build.VERSION_CODES.O)
    private void initEvent() {

        // 取消：关闭弹窗
        findViewById(R.id.cancel).setOnClickListener(view-> finish());


        // 确定：保存时间后关闭弹窗
        findViewById(R.id.confirm).setOnClickListener(view-> {
            AppConfig.setUpAndDownInterval(upAndDownIntervalValue);
            AppConfig.setGroupRestInterval(groupRestIntervalValue);

            if (AppConfig.save()) {
                finish();
            } else {
                ToastUtil.showText(this, "保存失败！");
            }
        });
    }




    private void setUpAndDownInterval(int value) {
        String val = value / 10 + "." + value % 10;
        upAndDownInterval.setText(val);
    }

    private void setGroupRestInterval(int value) {
        String val = value / 10 + "." + value % 10;
        groupRestInterval.setText(val);
    }
    


    @Override
    protected void onResume() {
        super.onResume();

        setUpAndDownInterval(upAndDownIntervalValue);
        setGroupRestInterval(groupRestIntervalValue);
    }
}